package com.coin900.coin.cmd.coin_coin;

import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.code.RespCode;
import com.coin900.coin.persist.coin.CoinCoinVO;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.service.coin.ICoinCoinService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shui
 * @create 2018-01-24
 **/
@Controller
public class CoinCoinPatchCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        CoinCoinVO coinCoinVO = new Gson().fromJson(source, CoinCoinVO.class);
        Long id = coinCoinVO.getId();
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("dataStatus", 1);
        List<CoinCoinVO> coinCoinVOS = this.coinCoinService.loadCoinCoin(params);
        if (coinCoinVOS.isEmpty()) {
            respSign.setMsg("币种不存在");
            respSign.setRespCode(RespCode.FAIL);
            return respSign;
        }
        coinCoinVO.setModifiedInfor(user);
        this.coinCoinService.updateCoinCoin(coinCoinVO);
        return respSign;
    }

    @Autowired
    private ICoinCoinService coinCoinService;

    @Override
    public String getCmdCode() {
        return CmdCode.COIN_COIN_PATCH;
    }
}
